package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.common.cache.GmallCache;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.mapper.SpuSaleAttrMapper;
import com.atguigu.gmall.product.model.*;
import com.atguigu.gmall.product.service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * title:
 * author: Gyh
 * date:2023-04-18 19:34
 * Description:
 */
@Service
public class SpuManageServiceImpl implements SpuManageService {

    @Autowired
    private SpuInfoService spuInfoService;

    @Autowired
    private SpuImageService spuImageService;

    @Autowired
    private SpuPosterService spuPosterService;

    @Autowired
    private SpuSaleAttrService spuSaleAttrService;

    @Autowired
    private SpuSaleAttrValueService spuSaleAttrValueService;

    @Autowired
    private BaseSaleAttrService baseSaleAttrService;

    /**
     * spu分页查询列表
     * @param iPage
     * @return
     */
    @Override
    public Page<SpuInfo> getSpuListByPage(Page<SpuInfo> iPage, Long category3Id) {
        //创建查询条件
        LambdaQueryWrapper<SpuInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpuInfo::getCategory3Id,category3Id);
        queryWrapper.orderByDesc(SpuInfo::getUpdateTime);
        //返回查询的数据
        return spuInfoService.page(iPage,queryWrapper);
    }

    /**
     * 获取销售属性数据
     * @return
     */
    @Override
    public List<BaseSaleAttr> getBaseSaleAttrList() {
        return baseSaleAttrService.list();
    }

    /**
     * 保存商品SPU信息
     * @param spuInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSpuInfo(SpuInfo spuInfo) {
        //将商品的基本信息封装到SpuInfo对象，存入spu_info表中
        spuInfoService.save(spuInfo);
        Long spuInfoId = spuInfo.getId();
        //将提交商品图片封装到SpiImage集合中，存入spu_image表中
        List<SpuImage> spuImageList = spuInfo.getSpuImageList();
        //判断集合不为空
        if (!CollectionUtils.isEmpty(spuImageList)){
            spuImageList.stream().forEach(spuImage -> {
                spuImage.setSpuId(spuInfoId);
            });
            //执行批量保存
            spuImageService.saveBatch(spuImageList);
        }
        //将提交海报图片封装到SpiPost集合中，存入spu_poster表中
        List<SpuPoster> spuPosterList = spuInfo.getSpuPosterList();
        if (!CollectionUtils.isEmpty(spuPosterList)){
            spuPosterList.stream().forEach(spuPoster -> {
                spuPoster.setSpuId(spuInfoId);
            });
            spuPosterService.saveBatch(spuPosterList);
        }
        //将提交的销售属性封装到SpiSaleAttr集合中，存入存入spu_sale_attr表中
        List<SpuSaleAttr> spuSaleAttrList = spuInfo.getSpuSaleAttrList();
        if (!CollectionUtils.isEmpty(spuSaleAttrList)){
            spuSaleAttrList.stream().forEach(spuSaleAttr -> {
                spuSaleAttr.setSpuId(spuInfoId);
                //将提交销售属性值SpuSaleAttrValue集合 存入spu_sale_attr_value表中
                List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttr.getSpuSaleAttrValueList();
                if (!CollectionUtils.isEmpty(spuSaleAttrValueList)){
                    spuSaleAttrValueList.stream().forEach(spuSaleAttrValue -> {
                        //关联商品spuid
                        spuSaleAttrValue.setSpuId(spuInfoId);
                        //为平台属性名赋值
                        spuSaleAttrValue.setSaleAttrName(spuSaleAttr.getSaleAttrName());
                    });
                    spuSaleAttrValueService.saveBatch(spuSaleAttrValueList);
                }
            });
            spuSaleAttrService.saveBatch(spuSaleAttrList);
        }
    }

    /**
     * 根据spuId 查询销售属性
     * @param spuId
     * @return
     */
    @Override
    public List<SpuSaleAttr> getSpuSaleAttrList(Long spuId) {
        SpuSaleAttrMapper spuSaleAttrMapper = (SpuSaleAttrMapper) spuSaleAttrService.getBaseMapper();
        List<SpuSaleAttr> list = spuSaleAttrMapper.getSpuSaleAttrList(spuId);
        return list;
    }

    /**
     * 根据spuId 获取spuImage集合 查询当前商品所有图片
     * @param spuId
     * @return
     */
    @Override
    public List<SpuImage> getSpuImageList(Long spuId) {
        LambdaQueryWrapper<SpuImage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpuImage::getSpuId,spuId);
        List<SpuImage> list = spuImageService.list(queryWrapper);
        return list;
    }


    /**
     * 根据spuId 获取海报数据
     * @param spuId
     * @return
     */
    @Override
    @GmallCache(prefix = "spuPoster:")
    public List<SpuPoster> getSpuPosterBySpuId(Long spuId) {
        LambdaQueryWrapper<SpuPoster> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpuPoster::getSpuId,spuId);
        List<SpuPoster> list = spuPosterService.list(queryWrapper);
        return list;
    }

    /**
     * 根据spuId,skuId 获取销售属性数据
     * @param skuId
     * @param spuId
     * @return
     */
    @Override
    @GmallCache(prefix = "spuSaleAttrList:")
    public List<SpuSaleAttr> getSpuSaleAttrListCheckBySku(Long skuId, Long spuId) {
        //获取销售属性持久层Mapper对象
        SpuSaleAttrMapper spuSaleAttrMapper = (SpuSaleAttrMapper) spuSaleAttrService.getBaseMapper();
        //调用自定义SQL查询
        List<SpuSaleAttr> list = spuSaleAttrMapper.getSpuSaleAttrListCheckBySku(skuId,spuId);
        return list;
    }

    /**
     * 根据spuId 获取到销售属性值Id 与skuId 组成的数据集
     * @param spuId
     * @return
     */
    @Override
    @GmallCache(prefix = "skuValueIdsMap:")
    public Map<String, Object> getSkuValueIdsMap(Long spuId) {
        HashMap<String, Object> map = new HashMap<>();
        //获取销售属性持久层Mapper对象
        SpuSaleAttrMapper spuSaleAttrMapper = (SpuSaleAttrMapper) spuSaleAttrService.getBaseMapper();
        //调用自定义SQL查询
        List<Map<String,Object>> mapList = spuSaleAttrMapper.getSkuValueIdsMap(spuId);
        if (!CollectionUtils.isEmpty(mapList)){
            mapList.stream().forEach(skuSaleAttrValuMap -> {
                String skuId = skuSaleAttrValuMap.get("sku_id").toString();
                String valueIds = skuSaleAttrValuMap.get("value_ids").toString();
                map.put(valueIds, skuId);
            });
        }
        return map;
    }
}
